package com.linkage.service.order;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.linkage.pojo.order.Order;
import com.linkage.pojo.order.OrderDetail;
import com.linkage.repository.order.OrderRepository;

/**
 * @标题：订单与支付中心
 * @描述：陈列订单详情以及支付操作
 * @author: 王权
 */
@Service
public class OrderService {

    @Autowired
    private OrderRepository orederRepository;

    // 新增一条订单
    public void addNewOrder(Order order) {
        if (!order.equals(null)) {
            this.orederRepository.add(order);
        }
    }

    // 修改一条订单
    public void modifyOrder(Order order) {
        if (!order.equals(null)) {
            this.orederRepository.update(order);
        }
    }

    // 修改订单状态
    public void modifyState(Integer state, Integer orderId) {
        this.orederRepository.updateState(state, orderId);
    }

    // 取消一条订单
    public void cancleOrder(Integer orderId) {
        OrderDetailService detail = new OrderDetailService();
        detail.deleteByOrder(orderId); // 删除该订单关联的所有商品信息
        this.orederRepository.cancel(orderId);

    }
    
    //修改订单 状态
    public void updateState(Integer state,Integer orderId){
        this.orederRepository.updateState(state, orderId);
    }
    
    //查询一条订单信息
    public Order loadOne(Integer orderId){
        return this.loadOne(orderId);
    }
    
    //查询该用户所有订单信息
    public List<Order> loadAll(Integer userId){
        return this.loadAll(userId);
    }
    
    //查询该用户所有状态为state的订单信息
    public List<Order> loadByState(Integer userId,Integer state){
        return this.loadByState(userId, state);
    }

    // 根据订单支付订单状态为state的订单,返回应付的金钱
    public float confirmPayForOrder(Order order,Integer state) {
        float money = 0;
        if(order.getState() != state){
            return 0;
        }
        OrderDetailService detailService = new OrderDetailService();
        List<OrderDetail> detail = detailService.loadById(order.getOrderId());//获取该订单所有商品信息
        for(int i = 0;!detail.isEmpty();i++){
            money += detail.get(i).getItemPrice();
        }
        return money;
    }

}
